package com.web.boot.global.controller;

import com.blue.core.auth.CaptchaUtil;
import com.blue.core.dict.HttpMethod;
import com.blue.core.util.RandomUtil;
import com.blue.http.annotation.Http;
import com.blue.http.message.Download;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.web.base.auth.exception.AppException;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.message.AppResponse;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.base.valid.UserLogin;
import com.web.base.valid.UserReg;
import com.web.boot.global.model.User;
import com.web.boot.global.util.HttpContext;
import com.web.core.cache.ConfigCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;

@Controller
@Http(url = "/app")
public class AppController
{
	private static Logger logger = LoggerFactory.getLogger(AppController.class);
	
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private AppFacade appFacade;
	@Resource
	private ConfigCache configCache;
	@Resource
	private HttpContext httpContext;
	
	public AppController()
	{
	}
	
	@Http(url = "/verify", method = HttpMethod.GET)
	public Download verify(HttpRequest request) throws IOException
	{
		return Download.createBuilder().handle(Download.Type.MEMORY, os ->
		{
			try
			{
				String valid = CaptchaUtil.gen(38, 5, RandomUtil.RandomType.UPPER_CASE_NUMBER, os);
				httpContext.setVerify(request, valid);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}).setFilename("verify.jpg").build();
	}

	@Http(url = "/login", method = HttpMethod.GET)
	public View loginGet(HttpRequest request)
	{
		return View.createBuilder().setView("/app/login").build();
	}

	@Http(url = "/login", method = HttpMethod.POST)
	public void loginPost(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		userRequest.setAppId(this.getAppId());
		userRequest.setIp(request.getIp());
		ValidationUtil.valid(request, UserLogin.class);
		UserResponse response = userFacade.login(userRequest);
		httpContext.setUser(request, User.toUser(response));
		logger.info("登录成功：{}", response.getNickname());
	}

	@Http(url = "/logout", method = HttpMethod.GET)
	public View logout(HttpRequest request) throws IOException
	{
		httpContext.setUser(request,null);
		String url = configCache.getValue("app_path");
		return View.createBuilder().setLocation(url + "/app/login").build();
	}

	@Http(url = "/reg", method = HttpMethod.GET)
	public View regGet(HttpRequest request)
	{
		return View.createBuilder().setView("/app/reg").build();
	}

	@Http(url = "/reg", method = HttpMethod.POST)
	public void regPost(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		userRequest.setAppId(this.getAppId());
		userRequest.setIp(request.getIp());
		ValidationUtil.valid(request, UserReg.class);
		UserResponse response = userFacade.reg(userRequest);
		httpContext.setUser(request, User.toUser(response));
		logger.info("注册成功：{}", response.getNickname());
	}
	
	private Integer getAppId()
	{
		String code = configCache.getValue("app_admin");
		AppResponse app = appFacade.getByCode(code);
		if (app == null)
			throw new AppException("应用不存在，code=" + code);
		
		return app.getId();
	}
	
}
